package com.src.cy.loadview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.src.cy.loadview.R;

/**
 * Created by CY on 2016/4/11.
 */
public class LoadView extends LinearLayout {

    private TextView mTvLoad;
    private int anim;

    public LoadView(Context context) {
        super(context);
        init(null);
    }

    public LoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }


    private void init(AttributeSet attrs) {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        CharSequence text;
        int style;

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.LoadView);
            text = typedArray.getText(R.styleable.LoadView_text);
            style = typedArray.getInt(R.styleable.LoadView_style, 0);
            anim = typedArray.getResourceId(R.styleable.LoadView_src, R.drawable.animation1);
            typedArray.recycle();
        } else {
            text = getResources().getString(R.string.loading);
            style = 0;
            anim = R.drawable.animation1;
        }


        if (style == 0) {
            inflate(getContext(), R.layout.load_progressbar, this);
        } else if (style == 1) {

            showLoadView(anim);
        }
        mTvLoad = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.load_textview, this, false);
        mTvLoad.setText(text);
        addView(mTvLoad);
    }

    public void showEmptyView() {
        ImageView iv = (ImageView) findViewById(R.id.iv);
        if (iv == null) {
            showLoadView(R.mipmap.empty);
        } else {
            iv.setImageResource(R.mipmap.empty);
        }

    }

    public void showLoadView() {

        ImageView iv = (ImageView) findViewById(R.id.iv);
        if (iv == null) {
            showLoadView(anim);
        } else {
            iv.setImageResource(anim);
            if (iv.getDrawable() instanceof AnimationDrawable) {
                AnimationDrawable animationDrawable = (AnimationDrawable) iv.getDrawable();
                animationDrawable.start();
            }
        }

    }

    public void setText(CharSequence text) {
        mTvLoad.setText(text);
    }

    public void setText(int text) {
        mTvLoad.setText(text);
    }

    private void showLoadView(int anim) {
        ImageView imageView = (ImageView) LayoutInflater.from(getContext()).inflate(R.layout.load_imageview, this, false);
        addView(imageView);
        imageView.setImageResource(anim);
        if (imageView.getDrawable() instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.start();
        }

    }

}
